﻿// -----------------------------------------------------------------------
// <copyright file="Ship_DAL.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace PMSGSC.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Ship_DAL: BaseDAL
    {
        // Lấy tất cả Ship
        public static List<Ship> GetAll()
        {
            DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.Ships);
            return DB.Ships.ToList();
        }
        // Lấy Ship theo Id
        public static Ship GetDataById(int id)
        {
            return DB.Ships.Where(s => s.Ship_ID == id).FirstOrDefault();
        }
        //Thêm một Ship
        public static bool Insert(Ship ship)
        {
            try
            {
                DB.Ships.InsertOnSubmit(ship);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Xóa một ship
        public static bool Delete(Ship ship)
        {
            try
            {
                DB.Ships.DeleteOnSubmit(ship);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Cập nhật ship
        public static bool Update(Ship ship)
        {
            try
            {
                //Ship objShip = GetDataById(ship.Ship_ID);
                //objShip.NameShip = ship.NameShip;
                //objShip.CodeShip = ship.CodeShip;
                //objShip.Details = ship.Details;
                //objShip.SelectDefault = ship.SelectDefault;
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
       
        // Kiểm tra xem Ship Code có tồn tại chưa ?
        public static bool IsExits(Ship ship)
        {
            Ship objShip = null;
            objShip = DB.Ships.Where(m => (m.CodeShip == ship.CodeShip) && (m.Ship_ID != ship.Ship_ID)).FirstOrDefault();
            return objShip != null ? true : false;
        }

    }
}
